package net.lab1024.smartadmin.module.baseModel.oss.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lab1024.smartadmin.common.anno.NoNeedLogin;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.module.baseModel.oss.entity.OssCallbackFileResult;
import net.lab1024.smartadmin.module.baseModel.oss.entity.OssCallbackResult;
import net.lab1024.smartadmin.module.baseModel.oss.entity.OssPolicyResult;
import net.lab1024.smartadmin.module.baseModel.oss.service.OssService;
import net.lab1024.smartadmin.module.baseModel.oss.service.impl.OssServiceImpl;
import net.lab1024.smartadmin.module.business.scenicSpot.service.ScenicSpotService;
import net.lab1024.smartadmin.module.support.imgFile.dao.ImgFileDao;
import net.lab1024.smartadmin.module.support.imgFile.domain.entity.ImgFileEntity;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * oss
 */
@RestController
@Api(tags = {"oss"})
@RequestMapping("/base/oss")
public class OssController extends BaseController {

    @Autowired
    private OssServiceImpl ossService;

    @Autowired
    private ImgFileDao imgFileDao;


    @ApiOperation(value = "oss上传签名生成")
    @GetMapping("/policy")
    public ResponseDTO<OssPolicyResult> policy() {
        OssPolicyResult result = ossService.policy();
        return ResponseDTO.succ().setData(result);
    }

    @ApiOperation(value = "oss上传成功回调")
    @PostMapping(value = "callback")
    @NoNeedLogin
    public ResponseDTO<OssCallbackFileResult> callback(HttpServletRequest request) {
        OssCallbackResult ossCallbackResult = ossService.callback(request);
        System.out.println("ossCallbackResult:::::" + ossCallbackResult);

        ImgFileEntity fileEntity = new ImgFileEntity();
        fileEntity.setFileSize(ossCallbackResult.getSize());
        String[] originalFilename = ossCallbackResult.getFilename().split("com");
        fileEntity.setFileName(originalFilename[1]);
        fileEntity.setFileUrl(ossCallbackResult.getFilename());
        fileEntity.setStatus(1);
        fileEntity.setFileType(ossCallbackResult.getMimeType());
        imgFileDao.insert(fileEntity);

        OssCallbackFileResult ossCallbackFileResult = new OssCallbackFileResult();
        ossCallbackFileResult.setFileId(fileEntity.getId());
        ossCallbackFileResult.setImgUrl(ossCallbackResult.getFilename());
       // ossCallbackFileResult.setImgUrl("http://wlddd.vaiwan.com" + originalFilename[originalFilename.length - 1]);

        return ResponseDTO.succ().setData(ossCallbackFileResult);
    }


}